/**
 * 
 */
package com.idea.auction.controller;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.idea.auction.constant.Const;
import com.idea.auction.model.Admin;
import com.idea.auction.model.Commodityorder;
import com.idea.auction.model.Dictionary;
import com.idea.auction.model.Permission;
import com.idea.auction.service.OrderService;
import com.idea.auction.service.DictionaryService;
import com.idea.auction.service.PermissionService;
import com.idea.auction.util.SysUtil;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;
import com.jfinal.server.Service;

/**
 * @author y.z
 */
@ControllerBind(controllerKey="/order",viewPath="/pages")
public class OrderController extends BaseController {
	private static final Logger log = Logger.getLogger(OrderController.class);
	@Service
	private OrderService OrderService;
	@Service
	private DictionaryService dictionaryService;
	/**
	 * 跳转到未付款
	 */
	@RequiresPermissions("order/toOrder")
	public void toOrder(){
		getNavigation();
		
		render("order/order-list.html");
	}
	
	/**
	 * 跳转到待发货
	 */
	@RequiresPermissions("order/toShip")
	public void toShip(){
		getNavigation();
		render("order/ship-list.html");
	}
	
	/**
	 * 跳转到完成
	 */
	@RequiresPermissions("order/toComplete")
	public void toComplete(){
		getNavigation();
		render("order/complete-list.html");
	}
	
	/**
	 * 跳转到确认完成
	 */
	@RequiresPermissions("order/toReceipt")
	public void toReceipt(){
		getNavigation();
		render("order/receipt-list.html");
	}
	
	/**
	 * 跳转到售后
	 */
	@RequiresPermissions("order/toAfterSale")
	public void toAfterSale(){
		getNavigation();
		//回收类型
		List<Dictionary> recoveryTypeList= dictionaryService.getListByCode("recovery_type");
		setAttr("recoveryTypeList", recoveryTypeList);
		render("order/after-sale-list.html");
	}
	
	
	/**
	 * 订单编辑页面
	 */
	public void toEditOrder(){
		Commodityorder order = new Commodityorder();
		
		if(!StrKit.isBlank(this.getPara("id"))){
			order = OrderService.getOrderById(this.getPara("id"));
		}
		//对象
		setAttr("order", order);
		
		//支付类型
		List<Dictionary> payList= dictionaryService.getListByCode("pay");
		setAttr("payList", payList);
		
		//订单状态
		List<Dictionary> orderStateList= dictionaryService.getListByCode("order_state");
		setAttr("orderStateList", orderStateList);
		
		//回收类型
		List<Dictionary> recoveryTypeList= dictionaryService.getListByCode("recovery_type");
		setAttr("recoveryTypeList", recoveryTypeList);
		
		//回收审批状态
		List<Dictionary> recoveryStateList= dictionaryService.getListByCode("recovery_state");
		setAttr("recoveryStateList", recoveryStateList);
		
		render("order/order-edit.html");
	}
	
	/**
	 * 获取订单分页
	 */
	public void getOrderList(){
		String orderNo = getPara("orderNo");
		String payType = getPara("payType");
		String orderState = getPara("orderState");
		String recoveryType = getPara("recoveryType");
		String recoveryState = getPara("recoveryState");
		
		if(!StrKit.isBlank(getPara("curPage"))){
			curPage = getParaToInt("curPage");
		}

		if(!StrKit.isBlank(getPara("orderNo"))){
			orderNo = getPara("orderNo");
		}
		
		if(!StrKit.isBlank(getPara("payType"))){
			payType = getPara("payType");
		}
		
		if(!StrKit.isBlank(getPara("orderState"))){
			orderState = getPara("orderState");
		}
		
		if(!StrKit.isBlank(getPara("recoveryType"))){
			recoveryType = getPara("recoveryType");
		}
		
		if(!StrKit.isBlank(getPara("recoveryState"))){
			recoveryState = getPara("recoveryState");
		}
		super.setPage(OrderService.getPage(curPage, orderNo, payType,  orderState , recoveryType , recoveryState));
	}
	
	
	/**
	 * 编辑订单
	 */
	public void editOrder(){
		String id = super.getPara("id");
		Commodityorder order = new Commodityorder();
		Admin admin = (Admin) getSession().getAttribute("admin");
		
		//获取数据
		String order_state = getPara("order_state");
		String express_company = getPara("express_company");
		String express_num = getPara("express_num");
		String recovery_type = getPara("recovery_type");
		String recovery_price = getPara("recovery_price");
		String recovery_state = getPara("recovery_state");
		
		
		order.set("id", Integer.parseInt(id)); //回收类型
		if(!StrKit.isBlank(order_state)){
			if(order_state.equals("4")){
				order.set("recovery_type", Integer.parseInt(recovery_type)); //回收类型
				order.set("recovery_price", Integer.parseInt(recovery_price)); //回收价格
				order.set("recovery_state", 1); //回收审批状态
			}
			order.set("order_state", Integer.parseInt(order_state)); //订单状态
		}
		if(!StrKit.isBlank(express_company)){
			order.set("express_company", express_company); //快递公司
		}
		if(!StrKit.isBlank(express_num)){
			order.set("express_num", express_num); //快递单号
		}
		
		
		if(!StrKit.isBlank(recovery_state)){
			order.set("recovery_state", Integer.parseInt(recovery_state)); //回收审批状态
		}
		order.set("update_name", admin.getStr("name"));
		order.set("update_date", new Date());
		
		if(OrderService.update(order)){
			msgUpdate();
		}else{
			msgSetVal(Const.SYS_ERROR_MSG);
		}
		
	}
	
	/**
	 * 删除订单
	 */
	public void delOrder(){
		String ids = getPara("ids");
		String idArray[] = ids.split(",");
		
		for(String id:idArray){
			OrderService.delete(id);
		}
		
		msgSuccess();
	}
	
	
}	